Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[python] New embeddings API #1023

Merged
merged 15 commits into from
Apr 1, 2024
Merged

[python] New embeddings API #1023

merged 15 commits into from
Apr 1, 2024

Conversation

ebezzi
Copy link
Member

@ebezzi ebezzi commented Feb 28, 2024

New embeddings API. Provides a unified access pattern to embeddings (regardless of whether they're collaboration or hosted) through get_anndata.

The Census version tag, e.g., ``"2023-12-15"``.

Returns:
A list of dictionaries, each containing metadata describing an available embedding.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Options:

  1. Return a subset of the metadata that only has relevant information (name, organism, etc). The example listed here is only for reference
  2. Return the full metadata.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strongly prefer 1) and having a verbose argument

@ebezzi ebezzi changed the title [python] New embeddings API [python] New embeddings API draft Feb 28, 2024
@@ -18,3 +19,7 @@ def _uri_join(base: str, url: str) -> str:
p_url.fragment,
]
return urllib.parse.urlunparse(parts)

def _extract_census_version(census: soma.Collection):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a live corpus unit test for this method. This should ensure that this parsing method remains consistent across releases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code has some lint - please run (an up to date) pre-commit across it

for emb in add_obs_embeddings:
emb_metadata = get_embedding_metadata_by_name(emb, organism, census_version, "obs_embedding")
uri = f"{CENSUS_EMBEDDINGS_LOCATION_BASE_URI}/{census_version}/{emb_metadata['id']}"
embedding = get_embedding(census_version, uri, obs_soma_joinids)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this will cause the census object to be re-opened. While this shouldn't be an issue, it will result into an extra call. With some effort I can refactor get_embedding to also accept an existing Census object, but I'm not sure if it's worth it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, you should refactor the code to have a (common, shared) function that accepts an already open Census handle

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@ebezzi ebezzi requested a review from bkmartinjr March 1, 2024 23:46
Copy link
Contributor

@bkmartinjr bkmartinjr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No real concerns with functionality, but suggest you do an API review with Pablo and Mike to figure out if the signatures are friendly/comprehensible from a UX perspective

@metakuni metakuni closed this Mar 8, 2024
@metakuni
Copy link
Contributor

metakuni commented Mar 8, 2024

Sorry, my clumsy fingers clicked the wrong button!

@metakuni metakuni reopened this Mar 8, 2024
@ebezzi ebezzi marked this pull request as ready for review March 15, 2024 16:46
@ebezzi ebezzi requested a review from bkmartinjr March 15, 2024 20:58

if add_obs_embeddings:
if obsm_layers and [x for x in add_obs_embeddings if x in obsm_layers]:
raise ValueError(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the (high) cost of calling query.to_anndata(), you should do all error checking before the costly ops - ie., move this kind of stuff to a prologue

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.

obs_soma_joinids = query.obs_joinids()
for emb in add_obs_embeddings:
emb_metadata = get_embedding_metadata_by_name(emb, experiment_name, census_version, "obs_embedding")
uri = f"{CENSUS_EMBEDDINGS_LOCATION_BASE_URI}/{census_version}/{emb_metadata['id']}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't these use urljoin()?

census_directory = get_census_version_directory()

if add_obs_embeddings:
if obsm_layers and [x for x in add_obs_embeddings if x in obsm_layers]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this error check (name collision) is missing from the var axis. Seems like you need it for both

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed varm_layers from the arguments, as you suggested, since it is not in the current API. The only way you can request a varm is through add_varm_embeddings

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right - gotcha!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I'm thinking about this previous conversation a bit more. Coming around to a slightly different perspective. Given:

  • tiledbsoma/somacore support more functionality than is exposed here (e.g., varp_layers, etc)
  • Census doesn't currently use these, due to schema definition (not code)
  • we want to clearly separate the arg names for clarity (the previous conversation where we decided to remove varm_layers)

I think we should land roughly here, to keep the code & schema modular:

  • this function (get_anndata) should pass through all (or at least most) arguments supported by ExperimentAxisQuery.to_anndata. That makes it future-proof and decoupled from the schema
  • the newly added args should have clearly separated names (the current names are not clear per above comment)
  • the error checking needs to detect collisions because AnnData only has one "dict" to shove them all in, and they might conflict.

Boiling this down, I suggest:

  1. Add (and pass to query.to_anndata) the args obsm_layers, varm_layers, obsp_layers, and varp_layers. These are just pass through.
  2. Rename add_obs_embeddings and add_var_embeddings to something clear (see above discussion)
  3. Do the error checks for collisions, and do them before you do any data loading. Example below.

Example error check for obs:

if set(obsm_layers) & set(add_obs_embeddings):
    ... there is a collision error ...

....do same for varm ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good.

response = requests.get(CELL_CENSUS_EMBEDDINGS_MANIFEST_URL)
response.raise_for_status()

versions = set()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this whole blob could be a simple comprehension which might make it more pythonic (this is a nit, up to you). E.g,

return sorted({ obj['census_version'] for obj in manifest.values() if ...  })

And unless there are duplicates expected, I'm not sure what the set adds? If there are duplicates, doesn't that imply you need more filter criteria?

Copy link
Member Author

@ebezzi ebezzi Mar 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The set is because multiple embeddings can exist for a single alias, and in this case we're only interested in the census version string, so it needs to be deduplicated. I'll rewrite using the comprehension.

Copy link

codecov bot commented Mar 18, 2024

Codecov Report

Attention: Patch coverage is 93.26425% with 13 lines in your changes are missing coverage. Please review.

Project coverage is 82.32%. Comparing base (a5dbdef) to head (520941e).
Report is 2 commits behind head on main.

Files Patch % Lines
...us/src/cellxgene_census/experimental/_embedding.py 87.09% 8 Missing ⚠️
...hon/cellxgene_census/src/cellxgene_census/_util.py 71.42% 2 Missing ⚠️
...xgene_census/tests/experimental/test_embeddings.py 95.23% 2 Missing ⚠️
...lxgene_census/src/cellxgene_census/_get_anndata.py 96.55% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1023      +/-   ##
==========================================
+ Coverage   81.33%   82.32%   +0.99%     
==========================================
  Files          73       74       +1     
  Lines        5566     5714     +148     
==========================================
+ Hits         4527     4704     +177     
+ Misses       1039     1010      -29     
Flag Coverage Δ
unittests 82.32% <93.26%> (+0.99%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@pablo-gar pablo-gar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ebezzi have you considered the convenience API to get all available Census version for a given embedding name?

The Census version tag, e.g., ``"2023-12-15"``.

Returns:
A list of dictionaries, each containing metadata describing an available embedding.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strongly prefer 1) and having a verbose argument

@ebezzi
Copy link
Member Author

ebezzi commented Mar 20, 2024

@ebezzi have you considered the convenience API to get all available Census version for a given embedding name?

@pablo-gar get_all_census_versions_with_embedding that would be the function.

@ebezzi ebezzi changed the title [python] New embeddings API draft [python] New embeddings API Mar 27, 2024
@ebezzi ebezzi merged commit c6cf312 into main Apr 1, 2024
15 checks passed
@ebezzi ebezzi deleted the ebezzi/new-embeddings-api branch April 1, 2024 22:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants